package com.xuexiang.xuidemo.dialog;

import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.BaseDialog;
import ohos.agp.window.dialog.PopupDialog;
import ohos.app.Context;

import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.provider.XulPopListProvider;
import com.xuexiang.xuidemo.util.TextUtils;


/**
 * pop工具类
 *
 * @since 2021-04-22
 */
public final class XUlPopUtil extends DirectionalLayout {
    private final static int POPMARROWIDTH = 50;
    private final static int POPMARROHEIGHT = 40;
    private Context mContext;
    private Component mLayout;
    private Component mComponent;
    private ListContainer container;
    private XulPopListProvider mXulPopListProvider;
    private PopupDialog mPopupDialog;
    private String mtvMsg;
    private Text tv_Msg;
    private int mWidth;
    private int mHeight;
    private int mMode;
    private int mArrowidth;
    private int mArrowheight;
    private boolean mIsListProvider;
    private PopDestroyedListener mPopDestroyedListener;

    private ListContainer.ItemClickedListener mOnItemSelectedListener;

    /**
     * 显示
     */
    public void show() {
        mLayout = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_xui_popup_layout, null, true);
        initDate();
        mPopupDialog = new PopupDialog(getContext(), mComponent);
        mPopupDialog.setCustomComponent(mLayout);
        mPopupDialog.setSize(mWidth, mHeight);
        mPopupDialog.setMode(mMode);
        mPopupDialog.setAutoClosable(true);
        mPopupDialog.setHasArrow(true);
        mPopupDialog.setArrowSize(mArrowidth, mArrowheight);
        mPopupDialog.setDialogListener(new BaseDialog.DialogListener() {
            @Override
            public boolean isTouchOutside() {
                dimiss();
                return true;
            }
        });
        mPopupDialog.show();
    }

    /**
     * 消失
     */
    public void dimiss() {
        if (mPopDestroyedListener != null && mPopupDialog != null) {
            mPopDestroyedListener.onPopDestroy();
        }
        mPopupDialog.destroy();
    }

    /**
     * 初始化数据
     */
    public void initDate() {
        container = (ListContainer) mLayout.findComponentById(ResourceTable.Id_list_container);
        tv_Msg = (Text) mLayout.findComponentById(ResourceTable.Id_tv_Msg);
        if (!mIsListProvider) {
            if (!TextUtils.isEmpty(mtvMsg)) {
                tv_Msg.setVisibility(Component.VISIBLE);
                tv_Msg.setText(mtvMsg);
            }
        } else {
            container.setVisibility(Component.VISIBLE);
            container.setItemProvider(mXulPopListProvider);
            container.setItemClickedListener(new ListContainer.ItemClickedListener() {
                @Override
                public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
                    mOnItemSelectedListener.onItemClicked(listContainer, component, i, l);
                    dimiss();
                }
            });
        }
    }

    /**
     * 销毁接口
     *
     * @since 2021-04-22
     */
    public interface PopDestroyedListener {
        /**
         * pop销毁时监听方法
         */
        void onPopDestroy();
    }


    /**
     * 获取构建者
     *
     * @param context
     * @return Builder 建造者对象
     */
    public static Builder builder(Context context) {
        return new Builder(context);
    }

    private XUlPopUtil(Builder builder) {
        super(builder.context);
        this.mContext = builder.context;
        this.mComponent = builder.params.mComponent;
        this.mXulPopListProvider = builder.params.mXulPopListProvider;
        this.mtvMsg = builder.params.mtvMsg;
        this.mWidth = builder.params.mWidth;
        this.mHeight = builder.params.mHeight;
        this.mMode = builder.params.mMode;
        this.mArrowidth = builder.params.mArrowidth;
        this.mArrowheight = builder.params.mArrowheight;
        this.mOnItemSelectedListener = builder.params.mOnItemSelectedListener;
        this.mPopDestroyedListener = builder.params.mPopDestroyedListener;
        this.mIsListProvider = builder.params.mIsListProvider;
    }

    /**
     * 参数类
     *
     * @since 2021-04-22
     */
    final static class Params {
        /**
         * 组件
         */
        public Component mComponent;
        /**
         * 迭代器
         */
        public XulPopListProvider mXulPopListProvider;
        /**
         * 显示字符信息
         */
        public String mtvMsg;
        /**
         * 宽度
         */
        public int mWidth;
        /**
         * 高度
         */
        public int mHeight;
        /**
         * 布局参数样式
         */
        public int mMode = LayoutAlignment.TOP;
        /**
         * 箭头宽度
         */
        public int mArrowidth = POPMARROWIDTH;
        /**
         * 箭头高度
         */
        public int mArrowheight = POPMARROHEIGHT;
        /**
         * 是否为迭代器
         */
        public boolean mIsListProvider;
        /**
         * 子对象点击监听接口
         */
        public ListContainer.ItemClickedListener mOnItemSelectedListener;
        /**
         * 弹窗销毁接口
         */
        public PopDestroyedListener mPopDestroyedListener;
    }

    /**
     * 构造基类
     */
    public static class Builder {
        private Params params = new Params();
        private final Context context;

        /**
         * 构造函数
         *
         * @param context
         */
        public Builder(Context context) {
            this.context = context;
        }

        /**
         * 设置布句组件
         *
         * @param mComponent
         * @return 当前构造者对象 Builder
         */
        public Builder setMComponent(Component mComponent) {
            this.params.mComponent = mComponent;
            return this;
        }

        /**
         * 设置迭代器
         *
         * @param mXulPopListProvider
         * @return 当前构造者对象 Builder
         */
        public Builder setMXulPopListProvider(XulPopListProvider mXulPopListProvider) {
            this.params.mXulPopListProvider = mXulPopListProvider;
            return this;
        }

        /**
         * 设置显示信息
         *
         * @param mtvMsg
         * @return 当前构造者对象 Builder
         */
        public Builder setMtvMsg(String mtvMsg) {
            this.params.mtvMsg = mtvMsg;
            return this;
        }

        /**
         * 设置是否迭代器
         *
         * @param isListProvider
         * @return 当前构造者对象 Builder
         */
        public Builder setIsListProvider(boolean isListProvider) {
            this.params.mIsListProvider = isListProvider;
            return this;
        }

        /**
         * 设置宽高
         *
         * @param mWidth
         * @param mHeight
         * @return 当前构造者对象 Builder
         */
        public Builder setSise(int mWidth, int mHeight) {
            this.params.mWidth = mWidth;
            this.params.mHeight = mHeight;
            return this;
        }

        /**
         * 设置箭头宽高
         *
         * @param mArrowidth
         * @param mArrowheight
         * @return 当前构造者对象 Builder
         */
        public Builder setArrowSize(int mArrowidth, int mArrowheight) {
            this.params.mArrowidth = mArrowidth;
            this.params.mArrowheight = mArrowheight;
            return this;
        }

        /**
         * 设置选中item监听
         *
         * @param onItemSelectedListener
         * @return 当前构造者对象 Builder
         */
        public Builder setMonItemSelectedListener(ListContainer.ItemClickedListener onItemSelectedListener) {
            this.params.mOnItemSelectedListener = onItemSelectedListener;
            return this;
        }

        /**
         * 设置点击item接口
         *
         * @param onItemSelectedListener
         * @return 当前构造者对象 Builder
         */
        public Builder setPopItemClickedListener(ListContainer.ItemClickedListener onItemSelectedListener) {
            this.params.mOnItemSelectedListener = onItemSelectedListener;
            return this;
        }

        /**
         * 设置 pop销毁接口
         *
         * @param mPopDestroyedListener
         * @return 当前构造者对象 Builder
         */
        public Builder setPopDestroyedListener(PopDestroyedListener mPopDestroyedListener) {
            this.params.mPopDestroyedListener = mPopDestroyedListener;
            return this;
        }

        /**
         * 展示的调用方法
         */
        public void show() {
            new XUlPopUtil(this).show();
        }
    }


}
